package nl.hu.verbrandendephoenix.controller;

import java.util.ArrayList;

import nl.hu.verbrandendephoenix.client.AppManager;
import nl.hu.verbrandendephoenix.client.NotificationManager;
import nl.hu.verbrandendephoenix.shared.FieldVerifier;
import nl.hu.verbrandendephoenix.shared.User;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;

public class EditProfile {

	private nl.hu.verbrandendephoenix.view.EditProfile profileForm;

	public EditProfile() {
		profileForm = new nl.hu.verbrandendephoenix.view.EditProfile();
	}
	
	public Panel getPanel() {
		Panel p = profileForm.constructForm();
		addHandlers();
		return p;
	}
	
	private void setData(User u) {
		profileForm.setUsername(u.getUserName());
		profileForm.setFirstName(u.getFirstName());
		profileForm.setNamePrefix(u.getNamePrefix());
		profileForm.setLastName(u.getLastName());
		profileForm.setGender(u.getGender());
		profileForm.setDateOfBirth(u.getDateOfBirth());
		profileForm.setHomePhone(u.getPhoneNumberHome());
		profileForm.setWorkPhone(u.getPhoneNumberWork());
		profileForm.setStreet(u.getStreet());
		profileForm.setStreetNumber(u.getStreetNumber());
		profileForm.setStreetNumberAdd(u.getStreetNumberAddition());
		profileForm.setCity(u.getCity());
		profileForm.setZipCode(u.getZIPCode());
	}
	
	public void getData() {
		final NotificationManager nM = NotificationManager.get();
		
		AppManager.get().getRpcService().getUserData(Cookies.getCookie("loginString"), new AsyncCallback<User>() {
			public void onFailure(Throwable caught) {
				nM.commError();
			}
			public void onSuccess(User result) {
				if(result != null) {
					setData(result);
				} else {
					nM.createNotification("Incorrecte gebruikersnaam of wachtwoord.");
				}
			}
		});
	}
	public void getData(String username) {
		final NotificationManager nM = NotificationManager.get();
		
		AppManager.get().getRpcService().getUserData(Cookies.getCookie("loginString"), username, new AsyncCallback<User>() {
			public void onFailure(Throwable caught) {
				nM.commError();
			}
			public void onSuccess(User result) {
				if(result != null) {
					setData(result);
				} else {
					nM.createNotification("Incorrecte gebruikersnaam of wachtwoord.");
				}
			}
		});
	}
	private void processSubmit() {
		final NotificationManager nM =  NotificationManager.get();
		if(!checkFields()) {
			nM.createNotification("U heeft niet alle velden correct ingevuld.");
		} else {
			User u = new User(profileForm.getLblUsername().getText(), 
					profileForm.getTxtFirstName().getText(),
					profileForm.getTxtNamePrefix().getText(),
					profileForm.getTxtLastName().getText(),
					profileForm.getGender(),
					profileForm.getTxtDateOfBirth().getValue(),
					profileForm.getTxtZipCode().getText(),
					profileForm.getTxtStreet().getText(),
					profileForm.getTxtStreetNumber().getText(),
					profileForm.getTxtStreetNumberAdd().getText(),
					profileForm.getTxtCity().getText(),
					profileForm.getTxtHomePhone().getText(),
					profileForm.getTxtWorkPhone().getText());
			
			AppManager.get().getRpcService().setUserData(Cookies.getCookie("loginString"), u, new AsyncCallback<Boolean>() {
				public void onFailure(Throwable caught) {
					nM.commError();
				}
				public void onSuccess(Boolean result) {
					if(result) {
						nM.createNotification("Uw gebruikersgegevens zijn succesvol opgeslagen.");
					} else {
						nM.commError();
					}
				}
			});
		}
	} 

	public boolean checkFields() {
		FieldVerifier fv = FieldVerifier.get();
		boolean b = true;

		TextBox txtFirstName = profileForm.getTxtFirstName();
		if(!fv.checkAlphaNumeric(txtFirstName.getText())) {
			fv.addRequired(txtFirstName);
			b = false;
		} else {
			fv.removeRequired(txtFirstName);
		}
		
		TextBox txtLastName = profileForm.getTxtLastName();
		if(!fv.checkAlphaNumeric(txtLastName.getText())) {
			fv.addRequired(txtLastName);
			b = false;
		} else {
			fv.removeRequired(txtLastName);
		}
		
		//TextBox txtDateOfBirth = profileForm.getTxtDateOfBirth();
		/*if(!fv.checkDateOfBirth(txtDateOfBirth.getText())) {
			fv.addRequired(txtDateOfBirth);
			b = false;
		} else {
			fv.removeRequired(txtDateOfBirth);
		}*/
		
		TextBox txtHomePhone = profileForm.getTxtHomePhone();
		if(txtHomePhone.getText().length() > 0 && !fv.checkPhoneNumber(txtHomePhone.getText())) {
			fv.addRequired(txtHomePhone);
			b = false;
		} else {
			fv.removeRequired(txtHomePhone);
		}
		
		TextBox txtWorkPhone = profileForm.getTxtWorkPhone();
		if(txtWorkPhone.getText().length() > 0 && !fv.checkPhoneNumber(txtWorkPhone.getText())) {
			fv.addRequired(txtWorkPhone);
			b = false;
		} else {
			fv.removeRequired(txtWorkPhone);
		}
		
		TextBox txtStreetNumber = profileForm.getTxtStreetNumber();
		if(txtStreetNumber.getText().length() > 0 && !fv.checkNumbers(txtStreetNumber.getText())) {
			fv.addRequired(txtStreetNumber);
			b = false;
		} else {
			fv.removeRequired(txtStreetNumber);
		}
		
		TextBox txtStreetNumberAdd = profileForm.getTxtStreetNumberAdd();
		if(txtStreetNumberAdd.getText().length() > 0 && !fv.checkAlphaNumeric(txtStreetNumberAdd.getText())) {
			fv.addRequired(txtStreetNumberAdd);
			b = false;
		} else {
			fv.removeRequired(txtStreetNumberAdd);
		}
		
		TextBox txtCity = profileForm.getTxtCity();
		if(txtCity.getText().length() > 0 && !fv.checkAlphaNumeric(txtCity.getText())) {
			fv.addRequired(txtCity);
			b = false;
		} else {
			fv.removeRequired(txtCity);
		}
		
		TextBox txtZipCode = profileForm.getTxtZipCode();
		if(txtZipCode.getText().length() > 0 && !fv.checkZipcode(txtZipCode.getText())) {
			fv.addRequired(txtZipCode);
			b = false;
		} else {
			fv.removeRequired(txtZipCode);
		}
		
		return b;
	}

	public void addHandlers() {
		Button btnConfirm = profileForm.getBtnConfirm();
		btnConfirm.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				processSubmit();
			} 
		});

		ArrayList<TextBox> arrTextBoxes = profileForm.getArrTextBox();
		KeyPressHandler kph = new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
					processSubmit();
				}
			}
		};
		
		for(TextBox tb : arrTextBoxes) {
			tb.addKeyPressHandler(kph);
		}
	}
}
